package com.check.controller;

import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.check.pojo.Dictionary;
import com.check.pojo.MarkStandard;
import com.check.pojo.MarkTable;
import com.check.pojo.MarkTableMeasure;
import com.check.pojo.MarkTableRule;
import com.check.pojo.PageObject;
import com.check.service.DictionaryService;
import com.check.service.MarkStandardService;
import com.check.service.MarkTableService;
import com.check.util.StringUtil;

@Controller
public class MarkTableController extends BaseController{

	@Autowired
	@Qualifier("markTableService")
	private MarkTableService markTableService;
	
	@Autowired
	@Qualifier("markStandardService")
	private MarkStandardService markStandardService;
	
	@Autowired
	@Qualifier("dictionaryService")
	private DictionaryService dictionaryService;
	
	@RequestMapping("markTableQuery.do")
	public ModelAndView markTableQuery(HttpServletRequest request,HttpServletResponse response,
			@ModelAttribute("pageObject")PageObject pageObject){
		
		return new ModelAndView("page/marktable/markTableList")
						.addObject("path",request.getContextPath());
	}
	
	@RequestMapping("tableQuery.do")
	public ModelAndView tableQuery(HttpServletRequest request,HttpServletResponse response,
			@ModelAttribute("pageObject")PageObject pageObject){
		if(pageObject.getCurrent()==0){
			pageObject.setCurrent(1);
		}
		List<MarkTable> tableList=markTableService.findAllTable(pageObject);
		pageObject.setUrl("tableQuery.do");
		pageObject.setTotal(markTableService.findTableCount());
		saveSysLog(RE_MATABLE, LOG_QUERY, request);
		return new ModelAndView("page/marktable/markTable")
						.addObject("path",request.getContextPath())
						.addObject("tableList",tableList)
						.addObject("page",pageObject.toPage());
	}
	
	@RequestMapping("toAddMarkTable.do")
	public ModelAndView toAddMarkTable(HttpServletRequest request,HttpServletResponse response){
		return new ModelAndView("page/marktable/markTableAdd")
					.addObject("path",request.getContextPath());
	}
	
	@RequestMapping("validMarkTable.do")
	public void validMarkTable(HttpServletRequest request,HttpServletResponse response){
		PrintWriter out=null;
		String tbId=request.getParameter("tbId");
		String flag = "0";
		try{
			response.setContentType("text/html;charset=gbk");
			out=response.getWriter();
			MarkTable findMarkTable = markTableService.findTableById(tbId);
			if(findMarkTable!=null){
				flag="1";
			}else{
				flag="0";
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			out.print(flag);
			if(out != null){
				out.flush();
				out.close();
			}
		}
		
	}
	
	@RequestMapping(value="addMarkTable.do",method = RequestMethod.POST)
	public ModelAndView addMarkTable(HttpServletRequest request,HttpServletResponse response,
			@ModelAttribute("markTable")MarkTable markTable){
			
				List<MarkTableMeasure>  list=markTable.getMarkTableMeasure(markTable.getTbId());
				markTableService.insertMarkTable(markTable,list);
				saveSysLog(RE_MATABLE, LOG_ADD, request);
				return new ModelAndView("redirect:/tableQuery.do")
							.addObject("path",request.getContextPath());
		}
	
	
	@RequestMapping(value="deleteMarkTable.do",method = RequestMethod.POST)
	public void deleteMarkTable(HttpServletRequest request,HttpServletResponse response,
			@ModelAttribute("markTable")MarkTable markTable){
			PrintWriter out = null;
			String flag = "0";
			
			try {
				out = response.getWriter();
				String tbId = request.getParameter("tbId");
				if(markTableService.deleteMarkTable(tbId)){
					flag = "1";
				}
				saveSysLog(RE_MATABLE, LOG_DELETE, request);
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				out.print(flag);
				if(out != null){
					out.flush();
					out.close();
				}
			}
		}
	
	@RequestMapping("toUpdateMarkTable.do")
	public ModelAndView toUpdateMarkTable(HttpServletRequest request,HttpServletResponse response){
		String tbId = request.getParameter("tbId");
		MarkTable findMarkTable = markTableService.findTableById(tbId);
		List<MarkTableMeasure> measureList = markTableService.findAllMeasure(tbId);
		return new ModelAndView("page/marktable/markTableUpdate")
					.addObject("path",request.getContextPath())
					.addObject("markTable",findMarkTable)
					.addObject("measureList",measureList);
	}
	
	@RequestMapping(value="updateMarkTable.do",method = RequestMethod.POST)
	public ModelAndView updateMarkTable(HttpServletRequest request,HttpServletResponse response,
			@ModelAttribute("markTable")MarkTable markTable){
				List<MarkTableMeasure> list = markTable.getMarkTableMeasure(markTable.getTbId());
				markTableService.updateMarkTable(markTable,list);
				saveSysLog(RE_MATABLE, LOG_UPDATE, request);
				return new ModelAndView("redirect:/tableQuery.do")
							.addObject("path",request.getContextPath());
		}
	
	
	
	@RequestMapping("measureQuery.do")
	public ModelAndView measureQuery(HttpServletRequest request,HttpServletResponse response,
			@ModelAttribute("pageObject")PageObject pageObject){
		String tbId = request.getParameter("tbId");
		if(pageObject.getCurrent()==0){
			pageObject.setCurrent(1);
		}
		List<MarkTableMeasure> tableMeasureList=markTableService.findAllMeasure(pageObject,tbId);
		pageObject.setUrl("measureQuery.do?tbId="+tbId);
		pageObject.setTotal(markTableService.findMeasureCount(tbId));
		return new ModelAndView("page/marktable/markTableMeasure")
						.addObject("tableMeasureList",tableMeasureList)
						.addObject("path",request.getContextPath())
						.addObject("page",pageObject.toPage())
						.addObject("tbId",tbId);
	}
	
	@RequestMapping("toAddMarkMeasure.do")
	public ModelAndView toAddMarkMeasure(HttpServletRequest request,HttpServletResponse response){
		String tbId = request.getParameter("tbId");
		return new ModelAndView("page/marktable/markTableMeasureAdd")
					.addObject("path",request.getContextPath())
					.addObject("tbId",tbId);
	}
	
	@RequestMapping(value="addMarkTableMeasure.do",method = RequestMethod.POST)
	public void addMarkTableMeasure(HttpServletRequest request,HttpServletResponse response,
			@ModelAttribute("markTableMeasure")MarkTableMeasure markTableMeasure){
			PrintWriter out = null;
			String flag = "0";
			
			try {
				out = response.getWriter();
			//	String tbMeasureScale = request.getParameter("tbMeasureScale");
			//	markTableMeasure.setTbMeasureSacle(Double.parseDouble(tbMeasureScale));
				markTableMeasure.setTmId(StringUtil.createUUID());
				markTableMeasure.setTbMeasure(URLDecoder.decode(request.getParameter("tbMeasure"),  "UTF-8"));
				markTableMeasure.setTbMeasureContent(URLDecoder.decode(request.getParameter("tbMeasureContent"),  "UTF-8"));
				if(markTableService.insertMarkTableMeasure(markTableMeasure)){
					flag = "1";
				}
				
				saveSysLog(RE_MARKMEASURE, LOG_ADD, request);
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				out.print(flag);
				if(out != null){
					out.flush();
					out.close();
				}
			}
		}

	@RequestMapping("toSetRule.do")
	public ModelAndView toSetRule(HttpServletRequest request,HttpServletResponse response){
		String tbId = request.getParameter("tbId");
		MarkTable markTable = markTableService.findTableById(tbId);
		List<Dictionary> type2List = dictionaryService.findByCode("type2");
		List<MarkStandard> markStandardList=new ArrayList<MarkStandard>();
		if(markTable!=null){
			 markStandardList= markStandardService.getMarkStdByStdType(markTable.getStandType()==null?"":markTable.getStandType());
		}
		List<MarkTableRule> markTableRuleList = markTableService.findAllMarkTableRule(tbId);
		return new ModelAndView("page/marktable/markTableRule")
					.addObject("path",request.getContextPath())
					.addObject("markTable",markTable)
					.addObject("markStandardList",markStandardList)
					.addObject("markTableRuleList",markTableRuleList)
					.addObject("type2List",type2List);
	}
	
	@RequestMapping("addMarkTableRule.do")
	public ModelAndView addMarkTableRule(HttpServletRequest request,HttpServletResponse response,
			@ModelAttribute("markTableRule")MarkTableRule markTableRule){
		List<MarkTableRule> markTableRuleList=markTableRule.getRuleList();
		markTableService.insertMarkTableRule(markTableRule.getTbId(),markTableRuleList);
		saveSysLog(RE_MARKTABLERULE, LOG_ADD, request);
		return new ModelAndView("redirect:/tableQuery.do");
	}
	
	
	@RequestMapping("deleteRule.do")
	public void deleteRule(HttpServletRequest request,HttpServletResponse response){
			PrintWriter out = null;
			String flag = "0";
			
			try {
				out = response.getWriter();
				String tbId = request.getParameter("tbId");
				if(markTableService.deleteMarkTableRule(tbId)){
					flag = "1";
				}
				saveSysLog(RE_MARKTABLERULE, LOG_DELETE, request);
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				out.print(flag);
				if(out != null){
					out.flush();
					out.close();
				}
			}
		}
	

		
}
